package com.huaxinkaiye.activity;

import com.baidu.mapapi.SDKInitializer;
import com.huaxinkaiye.R;
import com.huaxinkaiye.utils.ToastUtil;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity implements OnClickListener {
	private static String Tab_Home = "zhuye";
	private static String Tab_Account = "zhanghu";
	private static String Tab_Suggestion = "yijian";
	private static String Tab_Set = "shezhi";
	private static String Tab_More = "gengduo";
	private static TabHost mTabHost;
	private static final int COLOR1 = Color.parseColor("#787878");
	private static final int COLOR2 = Color.parseColor("#ffffff");
	private ImageView mBut1, mBut2, mBut3, mBut4, mBut5;
	private TextView mText1, mText2, mText3, mText4, mText5;

	private Intent mHomeItent, mAccountIntent, mSuggestionIntent, mSetIntent,
			mMoreIntent, mChanzhiIntent;
	// private int mCurTabId = R.id.channel1;
	private int mCurTabId;
	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;
	public static String roles;
	public LinearLayout channel1, channel2, channel3, channel4, channel5;

	/**
	 * 构造广播监听类，监听 SDK key 验证以及网络异常广播
	 */
	public class SDKReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			String s = intent.getAction();
			if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
				// text.setText("key 验证出错! 请在 AndroidManifest.xml 文件中检查 key 设置");
			} else if (s
					.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
				// text.setText("网络出错");
			}
		}
	}

	private SDKReceiver mReceiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		SDKInitializer.initialize(getApplicationContext());
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 注册 SDK 广播监听者
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
		iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
		mReceiver = new SDKReceiver();
		registerReceiver(mReceiver, iFilter);
		roles = getIntent().getStringExtra("roles");
		Log.e("main_roles", roles);
		// if(roles.contains("车辆建档")){
		// mCurTabId=R.id.channel1;
		// }else if(roles.contains("车辆初检")){
		// mCurTabId=R.id.channel2;
		// }else if(roles.contains("入场管理")){
		// mCurTabId=R.id.channel3;
		// }else if(roles.contains("拆解管理")){
		// mCurTabId=R.id.channel4;
		// }else if(roles.contains("议价申请")||roles.contains("议价审核")){
		// mCurTabId=R.id.channel5;
		// }
		prepareAnim();
		prepareIntent();
		setupIntent();
		prepareView();
	}

	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	private void prepareIntent() {
		mHomeItent = new Intent(this, JianDangActivity2.class);
		mAccountIntent = new Intent(this, ChuJianListActivity.class);
		mSuggestionIntent = new Intent(this, CarKuCunActivity.class);
		mSetIntent = new Intent(this, ChaiJieListActivity.class);
		mChanzhiIntent = new Intent(this, CanZhiActivity.class);
	}

	private void prepareView() {
		mBut1 = (ImageView) findViewById(R.id.imageView1);
		mBut2 = (ImageView) findViewById(R.id.imageView2);
		mBut3 = (ImageView) findViewById(R.id.imageView3);
		mBut4 = (ImageView) findViewById(R.id.imageView4);
		mBut5 = (ImageView) findViewById(R.id.imageView5);
		channel1 = (LinearLayout) findViewById(R.id.channel1);
		channel1.setOnClickListener(this);
		channel2 = (LinearLayout) findViewById(R.id.channel2);
		channel2.setOnClickListener(this);
		channel3 = (LinearLayout) findViewById(R.id.channel3);
		channel3.setOnClickListener(this);
		channel4 = (LinearLayout) findViewById(R.id.channel4);
		channel4.setOnClickListener(this);
		channel5 = (LinearLayout) findViewById(R.id.channel5);
		channel5.setOnClickListener(this);
		mText1 = (TextView) findViewById(R.id.textView1);
		mText2 = (TextView) findViewById(R.id.textView2);
		mText3 = (TextView) findViewById(R.id.textView3);
		mText4 = (TextView) findViewById(R.id.textView4);
		mText5 = (TextView) findViewById(R.id.textView5);
		if (!roles.contains("车辆建档")) {
			channel1.setClickable(false);
			mBut1.setImageResource(R.drawable.jiandang);
		}
		if (!roles.contains("车辆初检")) {
			channel2.setClickable(false);
			mBut2.setImageResource(R.drawable.chujian);
		}
		if (!roles.contains("入场管理")&!roles.contains("拆解管理")) {
			channel3.setClickable(false);
			mBut3.setImageResource(R.drawable.kucun);
		}
		if (!roles.contains("拆解管理")) {
			channel4.setClickable(false);
			mBut4.setImageResource(R.drawable.chaijie);
		}
		if (!roles.contains("议价申请") || !roles.contains("议价审核")) {
			channel5.setClickable(false);
			mBut5.setImageResource(R.drawable.canzhi);
		}
		
		if (roles.contains("车辆建档")) {
			mCurTabId = R.id.channel1;
			mTabHost.setCurrentTabByTag(Tab_Home);
			channel1.setClickable(true);
			mBut1.setImageResource(R.drawable.jiandang1);
		}
		else if (roles.contains("车辆初检")) {
			mCurTabId = R.id.channel2;
			mTabHost.setCurrentTabByTag(Tab_Account);
			channel2.setClickable(true);
			mBut2.setImageResource(R.drawable.chujian1);
		}
		else if (roles.contains("入场管理")) {
			mCurTabId = R.id.channel3;
			mTabHost.setCurrentTabByTag(Tab_Suggestion);
			channel3.setClickable(true);
			mBut3.setImageResource(R.drawable.kucun1);
		}
		else if (roles.contains("拆解管理")) {
			mCurTabId = R.id.channel3;
			mTabHost.setCurrentTabByTag(Tab_Suggestion);
			channel4.setClickable(true);
			channel3.setClickable(true);
			mBut3.setImageResource(R.drawable.kucun1);
			//mBut4.setImageResource(R.drawable.chaijie1);
		}
		else if (roles.contains("议价申请") || roles.contains("议价审核")) {
			mCurTabId = R.id.channel5;
			mTabHost.setCurrentTabByTag(Tab_More);
			channel5.setClickable(true);
			mBut5.setImageResource(R.drawable.caizhi1);
		}
	}

	private void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(Tab_Home, R.string.category_home,
				R.drawable.jiandang, mHomeItent));
		mTabHost.addTab(buildTabSpec(Tab_Account, R.string.category_set,
				R.drawable.chujian, mAccountIntent));
		mTabHost.addTab(buildTabSpec(Tab_Suggestion, R.string.category_search,
				R.drawable.kucun, mSuggestionIntent));
		mTabHost.addTab(buildTabSpec(Tab_Set, R.string.category_account,
				R.drawable.chaijie, mSetIntent));
		mTabHost.addTab(buildTabSpec(Tab_More, R.string.category_more,
				R.drawable.canzhi, mChanzhiIntent));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			mBut1.performClick();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mCurTabId == v.getId()) {
			return;
		}
		mBut1.setImageResource(R.drawable.jiandang);
		mBut2.setImageResource(R.drawable.chujian);
		mBut3.setImageResource(R.drawable.kucun);
		mBut4.setImageResource(R.drawable.chaijie);
		mBut5.setImageResource(R.drawable.canzhi);
		mText1.setTextColor(COLOR1);
		mText2.setTextColor(COLOR1);
		mText3.setTextColor(COLOR1);
		mText4.setTextColor(COLOR1);
		mText5.setTextColor(COLOR1);
		int checkedId = v.getId();
		final boolean o;
		if (mCurTabId < checkedId)
			o = true;
		else
			o = false;
		if (o)
			mTabHost.getCurrentView().startAnimation(left_out);
		else
			mTabHost.getCurrentView().startAnimation(right_out);
		switch (checkedId) {
		case R.id.channel1:
			// if (roles.contains("车辆建档")) {
			mTabHost.setCurrentTabByTag(Tab_Home);
			mBut1.setImageResource(R.drawable.jiandang1);
			mText1.setTextColor(COLOR2);
			// } else {
			// mBut1.setImageResource(R.drawable.jiandang2);
			// mText1.setTextColor(COLOR2);
			// ToastUtil.show(this, "没有权限");
			// }
			break;
		case R.id.channel2:
			// if (roles.contains("车辆初检")) {
			mTabHost.setCurrentTabByTag(Tab_Account);
			mBut2.setImageResource(R.drawable.chujian1);
			mText2.setTextColor(COLOR2);
			// } else {
			// mBut2.setImageResource(R.drawable.chujian2);
			// mText2.setTextColor(COLOR2);
			// ToastUtil.show(this, "没有权限");
			// }
			break;
		case R.id.channel3:
			// if (roles.contains("入场管理")) {
			mTabHost.setCurrentTabByTag(Tab_Suggestion);
			mBut3.setImageResource(R.drawable.kucun1);
			mText3.setTextColor(COLOR2);
			// } else {
			// mBut3.setImageResource(R.drawable.kucun2);
			// mText3.setTextColor(COLOR2);
			// ToastUtil.show(this, "没有权限");
			// }
			break;
		case R.id.channel4:
			// if (roles.contains("拆解管理")) {
			mTabHost.setCurrentTabByTag(Tab_Set);
			mBut4.setImageResource(R.drawable.chaijie1);
			mText4.setTextColor(COLOR2);
			// } else {
			// mBut4.setImageResource(R.drawable.chaijie2);
			// mText4.setTextColor(COLOR2);
			// ToastUtil.show(this, "没有权限");
			// }
			break;
		case R.id.channel5:
			// if (roles.contains("议价申请")||roles.contains("议价审核")) {
			mTabHost.setCurrentTabByTag(Tab_More);
			mBut5.setImageResource(R.drawable.caizhi1);
			mText5.setTextColor(COLOR2);
			// } else {
			// mBut5.setImageResource(R.drawable.canzhi2);
			// mText5.setTextColor(COLOR2);
			// ToastUtil.show(this, "没有权限");
			// }
			break;
		default:
			break;
		}

		if (o)
			mTabHost.getCurrentView().startAnimation(left_in);
		else
			mTabHost.getCurrentView().startAnimation(right_in);
		mCurTabId = checkedId;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		// 取消监听 SDK 广播
		unregisterReceiver(mReceiver);
		super.onDestroy();
	}

}
